/**
 * @file src/datatypes/Path.java
 */
package datatypes;

/** imports */
import java.util.Vector;

/**
 * Simple path class
 */
public class Path implements Comparable<Path> 
{
	/** the nodes within this path */
	private Vector<Node> mNodes;
	
	/**
	 * Constructor
	 */
	public Path()
	{
		mNodes = new Vector<Node>();
	}
	
	@SuppressWarnings("unchecked")
	public Path(Path other)
	{
		mNodes = (Vector<Node>) other.mNodes.clone();
	}

	/**
	 * Get size of path 
	 * @return size of path
	 */
	public int size() { return mNodes.size(); }
	
	/**
	 * Get a Node 
	 * @param i Which one
	 * @return Node
	 */
	public Node get(int i)
	{
		if (size() > i) { return mNodes.get(i); }
		return null;
	}
	
	/**
	 * Getter for costs
	 * 
	 *  @return Path costs or Float.MAX_VALUE if path is empty
	 */
	public float getCosts()
	{
		if (size() > 0) { return getLast().getPathCost(); }
		return Float.MAX_VALUE;
	}

	/**
	 * Getter for F (returning the F value of last node in path
	 * 
	 * @return F value or Float.MAX_VALUE if path is empty
	 */
	public float getF()
	{
		if (size() > 0) { return getLast().getF(); }
		return Float.MAX_VALUE;
	}
	
	/**
	 * Getter for depth
	 *
	 * @return Depth of this path
	 */
	public int getDepth() { return size(); }
	
	/**
	 * Getter for last Node in path
	 *
	 * @return last Node in path or null if path is empty
	 */
	public Node getLast()
	{
		if (size() > 0) { return mNodes.get(mNodes.size() - 1); }
		return null;
	}
	
	/**
	 * Contains test 
	 */
	public boolean contains(Node n)
	{
		assert(n != null);
		
		for (Node a: mNodes) {
			if (a.getState().compareTo(n.getState()) == 0) { return true; }
		}
		return false;

	}
	
	/**
	 * Append a Node to path
	 * 
	 * @param
	 */
	public void append(Node n)
	{
		assert(n != null);
		mNodes.addElement(n);
	}

	/**
	 * Override compareTo()
	 * 
	 * @param other Other path to compare to
	 * 
	 * @return -1 0 or 1 depending on comparison
	 */
	public int compareTo(Path other)
	{
		assert(other != null);
		
		float a = getLast().getF();
		float b = other.getLast().getF();
		if (a < b) {
			return -1;
		} else if (a > b) {
			return 1;
		}
		return 0;
	}

    /**
     * Get string representation of a given path (Vector<Node>)
     * 
     * @return String representation of the path
     */
    public String toString()
    {
    	String result = "Path = { ";
    	
    	if (mNodes.size() >= 1) {
    		result += mNodes.get(0).toString();
    	}

    	if (mNodes.size() > 1) {
    		for (int i = 1; i < mNodes.size(); ++i) {
    			result += " -> ";
    			result += mNodes.get(i).toString();
    		}
    	}
    	
    	result += " }";
    	return result;
   	
    } // toString
    
} // class Path
